<?php
if (isset($_REQUEST["id"]) && getTransactionPermission($_REQUEST["id"], $_SESSION["userid"]))
{
	if (isset($_REQUEST["modify"]))
	{
		$success = true;
		$validator = array("bookingtime" => "isPostedDatetime",
							"amount" => "isPostedInteger",
							"accountid" => "isPosted",
							"categoryid" => "isPosted");

		if (validateForm($validator, $_REQUEST))
		{
			if (!modifyTransaction($_REQUEST["transactionid"], $_REQUEST["accountid"], $_SESSION["userid"], $_REQUEST["direction"], $_REQUEST["amount"], $_REQUEST["description"], $_REQUEST["bookingtime"], $_REQUEST["categoryid"], $_REQUEST["tags"], $_REQUEST["partnername"]))
			{
				$success = false;
			}
		}
		else
		{
			$success = false;
		}
	}

	$selectedTransaction = getTransaction($_REQUEST["id"]);
	$accounts = getAccounts($_SESSION["userid"]);
	$categoryTree = getCategoryTree("NULL", 1, false);
?>
			<div id="textcontainer">
				<h2>Tranzakció módosítása</h2>
				<p>
					<?php if (isset($success) && !$success) print "<div class=\"errortitle\">valami nem sikerult!</div>";
						else if (isset($success)) print "<div class=\"errortitle\">Sikerult!</div>";?>
					<form name="newtransaction" action="<?php print $_SERVER["SCRIPT_NAME"] . "?" . $_SERVER["QUERY_STRING"];?>" method="post">
						<input type="hidden" name="transactionid" value="<?php print $_REQUEST["id"];?>"/>
						számla: <select name="accountid">
<?php
	foreach ($accounts as $acc)
	{
		print "<option value=\"" . $acc["id"] . "\"" . (($selectedTransaction["accountid"] == $acc["id"])?" selected=\"selected\"":"") . ">" . $acc["name"] . "</option>";
	}
?>
						</select><br/>
						irány: <select name="direction">
						<option value="E"<?php print ($selectedTransaction["amount"] < 0)?" selected=\"selected\"":"";?>>kiadás</option>
						<option value="I"<?php print ($selectedTransaction["amount"] > 0)?" selected=\"selected\"":"";?>>bevétel</option>
						</select><br/>
						összeg: <input type="text" name="amount" value="<?php print abs($selectedTransaction["amount"]);?>"/><br/>
						partner: <input type="text" name="partnername" value="<?php print $selectedTransaction["partnername"];?>"/><br/>
						leírás: <textarea name="description"><?php print $selectedTransaction["description"];?></textarea><br/>
						időpont: <input type="text" name="bookingtime" value="<?php print $selectedTransaction["bookingtime"];?>"/><i>(pl. 2005-10-12 15:00:00)</i><br/>
						címke: <input type="text" name="tags" value="<?php print $selectedTransaction["tags"];?>"/><br/>
<?php
	if (!isset($selectedTransaction["split"]))
	{
		print 'kategória: <select name="categoryid">';
		foreach ($categoryTree as $category)
		{
			print "<option value=\"" . $category["category"]["id"] . "\"" . (($selectedTransaction["categoryid"] == $category["category"]["id"])?" selected=\"selected\"":"") . ">" . $category["category"]["name"] . "</option>";
			if (isset($category["subcategoryList"]))
			{
				foreach($category["subcategoryList"] as $subcategory)
				{
					print "<option value=\"" . $subcategory["category"]["id"] . "\"" . (($selectedTransaction["categoryid"] == $subcategory["category"]["id"])?" selected=\"selected\"":"") . ">&nbsp;&nbsp; - " . $subcategory["category"]["name"] . "</option>";
				}
			}
		}
		print '</select><br/>';
	}
	else
	{
		print '<input type="hidden" name="categoryid" value="split"/>';
		foreach($selectedTransaction["split"] as $split)
		{
			print "<hr/>";
			print 'összeg: ' . $split["splitamount"] . '<br/>';
			print 'kategória: ' . getCategoryString($categoryTree, $split["categoryid"]) . "<br/>";
		}
	}
	?>
						<br/>
						<input type="submit" name="modify" value="Módosít"/>
					</form>
				</p>
			</div>
			<!---->
<?php
}
else
{
	print '<div id="textcontainer">nincs hozzáférési joga</div>';
}
?>
